import { _decorator, Component, Node, ScrollView, Sprite, SpriteFrame } from 'cc';
import { UIBase } from '../../FrameWork3.8.6/FrameWork/UIFramework/UIBase';
import { PlayerPropertyBase } from './PlayerPropertyBase';
import { Emitter } from '../../FrameWork3.8.6/FrameWork/Common/Emitter';
import { EquipmentDt } from '../../FrameWork3.8.6/FrameWork/Common/CommonInterface';
import { GameManager } from '../Game/Manager/GameManager';
import { DataMgr } from '../../FrameWork3.8.6/FrameWork/Data/DataMgr';
import { EquipmentDtMgr } from '../../FrameWork3.8.6/FrameWork/Data/DataBaseMgr';
import { GridButton } from '../Game/Object/GridButton';
import { BagManager } from '../Game/Manager/BagManager';
import { ResMgr } from '../../FrameWork3.8.6/FrameWork/Res/ResMgr';
import { BundleConfig } from '../../FrameWork3.8.6/FrameWork/NeedToChange/BundleConfig';
const { ccclass, property } = _decorator;

@ccclass('EquipmentPanel')
export class EquipmentPanel extends PlayerPropertyBase {
    //初始化关卡数据管理器
    private _equipmentDtMgr:EquipmentDtMgr=null;

    //存储从背包数据管理者获取的数据
    private _arrEquipmentDt:EquipmentDt[]=null;

    //管理当前网格的容器，判断是否有相同的，如果有则在它下面的文字+1
    private _mapGrid:Map<number,GridButton>=new Map();
    initData(): void {
        this._equipmentDtMgr=DataMgr.instance.getMgr<EquipmentDtMgr>("EquipmentDtMgr");
        //注册更新装备的方法
        Emitter.instance.on("refreshEquipmentDt",this.refreshEquipmentDt,this);
    }

    onEnter(...rest: any[]): void {
        this.refreshEquipmentGrid();
        this.refreshEquipmentDt();
    }

    //更新网格的方法
    refreshEquipmentGrid(){
        //先将当前节点身上的网格清空
        let content=this.getComp<ScrollView>("_ScrollView","ScrollView").content;
        content.removeAllChildren();
        //先获取装备数据列表
        this._arrEquipmentDt=BagManager.instance.getBagDt<EquipmentDt>("Equipment",false);
        if(!this._arrEquipmentDt){
            return;
        }
        for(let equipmentDt of this._arrEquipmentDt){
            //判断当前容器中是否已经存在，如果存在，则无需实例化
            let gridBtn=this._mapGrid.get(equipmentDt.id);
            if(gridBtn){
                gridBtn.num+=1;
                gridBtn.onInit(equipmentDt);
                //当前循环结束
                continue;
            }

            //没有重复的，实例化节点，设置父亲
            let grid=this._gridBtnNodePool.alloc();
            grid.parent=content;
            let gridTs=grid.getComponent(GridButton);
            gridTs.onInit(equipmentDt)
            this._mapGrid.set(equipmentDt.id,gridTs);
        }

        //遍历容器中的节点添加点击事件
        for(let value of this._mapGrid.values()){
            //先移除身上注册的方法
            value.node.targetOff(value.node);
            //注册事件
            value.node.on("click",()=>{
                this.openDetailsPanel(value.curPropertyDt,value.num);
            })
        }

    }
    //更新已装备数据的方法
    refreshEquipmentDt(){
        //先获取背包数据的已经装备的数据列表
        let arrEquipDt=BagManager.instance.getBagDt("Equipment",true);
        if(!arrEquipDt){
            return;
        }
        //遍历数组依次设置给按钮的图片并且给按钮注册点击事件
        for(let equipDt of arrEquipDt){
            //先通过id找到对应节点
            //通过模运算计算图标
            let lastNum=parseInt(equipDt.id.toString().charAt(3));
            let index=(lastNum-1)%3+1;
            let equiBtn=this.getNode("_EquipBtn"+index);
            //设置对应图片
            equiBtn.getComponent(Sprite).spriteFrame=ResMgr.instance.getRes<SpriteFrame>(BundleConfig.Img.name,equipDt.iconName);
            //先移除身上注册的方法
            equiBtn.targetOff(equiBtn); 
            //注册点击事件
            equiBtn.on("click",()=>{
                this.openEquipDetailsPanel(equipDt);
            },equiBtn)
        }
    }

    //关闭时清空
    onExit(...rest: any[]): void {
        this._mapGrid.clear();
    }
}


